// pages/index/index.js
var api = require('../../utils/api.js')
var app = new getApp()
import * as video from '../../utils/video';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navH: 0,
    isIphoneX: false,
    path:api.path,
    homeData:[],
    productItems:[],
    active:0,
    productData:[{
      title:"750X1500 MM",
      productList:[]
    },{
      title:"800X2400 MM",
      productList:[]
    },{
      title:"800X2600 MM",
      productList:[]
    }],
    brandArr:[{
      img:'/imgs/1.png',
      text:'新岩素品牌宣传片1'
    },{
      img:'/imgs/2.png',
      text:'新岩素品牌宣传片2'
    },{
      img:'/imgs/3.png',
      text:'新岩素品牌宣传片3'
    },{
      img:'/imgs/4.png',
      text:'新岩素品牌宣传片4'
    },],
    newArr:[],
    hotArr:[],
    index: true,
    show: false,
    bannerImgs: [],
    brandImgs: [],
    current: 0,
    current1: 0,
    current2: 0,
    content: null,
    content1: null,
    content2: null,
    ishow: false,
    videoSrc: 'http://xcx.hbiceramiche.com/HBI.mp4',
    isplay:false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getHomeData('','onShow')
    //登录
    // wx.login({
    //   success: res => {
    //     var url = 'mp_login'
    //     var data = {
    //       code: res.code
    //     }
    //     api.request(url, 'POST', data).then(res => {
    //       if (res.code == 200) {
    //         wx.setStorageSync('token', res.token)
    //         wx.setStorageSync('user', res.user)
    //         this.getData()
    //         if (res.user.nick_name && res.user.img) {
    //           wx.setStorageSync('bigs_close', true)
    //           wx.setStorageSync('istake', true)
    //           this.bindclose()
    //         } else {
    //           //弹出新人领取礼包弹窗
    //           let istake = api.cache('istake')
    //           if (!istake) {
    //             this.setData({
    //               ishow: true
    //             })
    //           }
    //         }
    //         let userInfo = api.cache('user')
    //       }
    //     })
    //   }
    // })
    //template.tabbar("tabBar", 0, this)
    //this.getHomeData()
    let isIphoneX = app.globalData.isIphoneX
    if (isIphoneX) {
      this.setData({
        isIphoneX
      })
    }
    this.setData({
      navH: app.globalData.navHeight
    })
  },
  select(e){
    let id = e.currentTarget.dataset.id
    // this.setData({
    //   active : id
    // })
    this.getHomeData(id,"select")
  },
  getHomeData(id,type){
    let params = {
      id
    }
    //10启动广告
    api.post('home',params).then(res => {
      if (res.code == 200) {
        if(type == 'onShow'){
          let productItems = this.data.productItems
          productItems.push(
            ...res.data.goods_classfiy_lst
          )
          this.setData({
            productItems
          })
        }
        let arr = []
        let goodList = res.data.goods_spec_lst
        for (const key in goodList) {
          if (Object.hasOwnProperty.call(goodList, key)) {
            let obj = {}
            obj['title'] = key
            obj['productList'] = goodList[key]
            arr.push(obj)
          }
        }
        this.setData({
          productData: arr,
          homeData:res.data
        })
      }
    })
  },
  toPlay(e) {
    let index = e.detail
    let src = this.data.path + this.data.homeData.videos[index].video_url;
    //足迹
    api.saveFoot(this.data.homeData.videos[index].id,3)
    //跳转到全屏播放页面
    wx.navigateTo({
      url: '/pages/videoFull/videoFull?src='+src,
    })
  },
  
  bindchange(e) {
    this.setData({
      current1: e.detail.current
    })
  },
  toBrand(e) {
    let index = e.currentTarget.dataset.index
    if(index == 0){
      wx.navigateTo({
        url: './brand/brand',
      })
    }else if(index == 1){
      wx.reLaunch({
        url: '/pages/goods/index',
      })
    }else if(index == 2){
      wx.reLaunch({
        url: '/pages/case/index',
      })
    }else if(index == 3){
      wx.navigateTo({
        url: './case/case',
      })
    }
    
  },
  toNewMore() {
    // let res = api.checkLogin()
    // if (!res) {
    //   return;
    // }
    wx.reLaunch({
      url: '/pages/goods/index',
    })
  },
  toHotMore() {
    // let res = api.checkLogin()
    // if (!res) {
    //   return;
    // }
    wx.reLaunch({
      url: '/pages/case/index',
    })
  },
  toPushGoods() {
    // let res = api.checkLogin()
    // if (!res) {
    //   return;
    // }
    wx.navigateTo({
      url: './newGoods/newGoods',
    })
  },
  toBrandMore(){
    wx.navigateTo({
      url: './brandVideo/index',
    })
  },
  goto(e){
    let index = e.currentTarget.dataset.type
    let item  = this.data.homeData.slides[index]
    if(item.source==1){
      wx.reLaunch({
        url: item.page_config.page_url,
      })
    }else{
      wx.navigateTo({
        url: './detail/detail?url='+item.url,
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },
  toDetail(e) {
    let id = e.currentTarget.dataset.id
    //api.saveFoot(id,1)
    wx.navigateTo({
      url: '/pages/goods/index?id=' + id,
    })
  },
  toDetail1(e) {
    let item = e.currentTarget.dataset.item.goods
    let id = item.id
    if(item.is_soft==1){
      wx.navigateTo({
        url: `/pages/goods/detail/detail?id=${id}&type=1`,
      })
    }else{
      wx.navigateTo({
        url: '/pages/goods/detail/detail?id=' + id,
      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      // imageUrl: '../../imgs/index/share.jpg',
      // title: app.globalData.shareTitle,
      // path: '/pages/index/index'
    }
  }
})